﻿using System;
using System.Data;
using System.Collections.Generic;
using ND.Tool;
using TableFieldInfo = ND.Tool.DB4CodeAuto.TableFieldInfo;
using TableInfo = ND.Tool.DB4CodeAuto.TableInfo;
using System.Linq;

namespace AutoCode.Win
{
    public class DB4CodeAuto : ND.Tool.DB4CodeAuto
    {
        private string m_cs = ConfigInfo.WebCS;

        /// <summary>
        /// 获取数据库特定表所有字段信息(sql2005)
        /// 解决因为索引导致的模板生成异常
        /// </summary>
        /// <param name="p_tableName">表名</param>
        /// <returns></returns>
        public new IList<TableFieldInfo> TableFieldListGet(string p_tableName)
        {
            //try
            //{
                if (p_tableName.ToLower().Contains("ez_video"))
                {

                }
                var list = base.TableFieldListGet(p_tableName);
                var list2 = TableFieldListFilter(list, p_tableName);
                return list2;
            //}
            //catch (Exception exp)
            //{
            //    return null;
            //}
        }

        /// <summary>
        /// 过滤因为索引导致的模板生成异常
        /// 可能还有其他的原因导致生成异常，需要后续跟踪解决
        /// </summary>
        /// <param name="p_tableName">表名</param>
        /// <returns></returns>
        public IList<TableFieldInfo> TableFieldListFilter(IList<TableFieldInfo> tablefieldinfos,string p_tableName)
        {
            DataBase db = SQLHelper.CreateData(m_cs);
            string sql = "SELECT A.name AS table_name, A.id AS table_id, B.name AS col_name, B.colid AS col_id, B.xtype AS col_type, ISNULL(C.name, '-') AS prop_name,C.value AS prop_value FROM sys.sysobjects AS A INNER JOIN sys.syscolumns AS B ON A.id = B.id left JOIN sys.extended_properties AS C ON C.major_id = A.id AND C.minor_id = B.colid where A.name=@table_name  and (c.class_desc <>'index' or C.class_desc is null)";
            db.AddParameter("@table_name", p_tableName);
            DataSet ds = db.ExecuteDataSet(sql);

            List<TableFieldInfo> list = new List<TableFieldInfo>();
            //string str1 = "", str2 = "";
            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                //base.FormatFieldType(Convert.ToString(dataRow["col_type"]), out str1, out str2);
                TableFieldInfo tableFieldInfo = new TableFieldInfo();
                tableFieldInfo.TableName = Convert.ToString(dataRow["table_name"]);
                tableFieldInfo.ColName = Convert.ToString(dataRow["col_name"]);
                //tableFieldInfo.ColType = str1;
                //tableFieldInfo.ColConvertType = str2;
                //tableFieldInfo.PropValue = Convert.ToString(dataRow["prop_value"]);
                var find = (from o in tablefieldinfos where o.ColName == tableFieldInfo.ColName select o).FirstOrDefault();
                if(find != null)
                    list.Add(find);
            }

            if (list != null && list.Count > 0)
                return list;
            else
                return null;
        }

        /// <summary>
        /// 格式化字段类型
        /// </summary>
        /// <param name="p_fieldType"></param>
        /// <returns></returns>
        private void FormatFieldType(string p_fieldType, out string p_str1, out string p_str2)
        {
            string str1 = "", str2 = "";
            switch (p_fieldType)
            {
                case "167": //varchar(50)
                    str1 = "string";
                    str2 = "Convert.ToString";
                    break;
                case "127": //bigint
                    str1 = "long";
                    str2 = "Convert.ToInt64";
                    break;
                case "231": //nvarchar(50)
                    str1 = "string";
                    str2 = "Convert.ToString";
                    break;
                case "106": //decimal(18, 0)
                    str1 = "decimal";
                    str2 = "Convert.ToDecimal";
                    break;
                case "108": //numeric(18, 2)
                    str1 = "decimal";
                    str2 = "Convert.ToDecimal";
                    break;
                case "104": //bit
                    str1 = "bool";
                    str2 = "Convert.ToBoolean";
                    break;
                case "56": //int
                    str1 = "int";
                    str2 = "Convert.ToInt32";
                    break;
                case "58": //smalldatetime
                    str1 = "DateTime";
                    str2 = "Convert.ToDateTime";
                    break;
                case "60": //money
                    str1 = "decimal";
                    str2 = "Convert.ToDecimal";
                    break;
                case "61": //datetime
                    str1 = "DateTime";
                    str2 = "Convert.ToDateTime";
                    break;
                case "99": //ntext
                    str1 = "string";
                    str2 = "Convert.ToString";
                    break;
                case "35": //text
                    str1 = "string";
                    str2 = "Convert.ToString";
                    break;
                case "34": //image
                    str1 = "byte[]";
                    str2 = "(byte[])";
                    break;
                case "165": //varbinary
                    str1 = "byte[]";
                    str2 = "(byte[])";
                    break;
                case "175": //char
                    str1 = "string";
                    str2 = "Convert.ToString";
                    break;
            }
            p_str1 = str1;
            p_str2 = str2;
        }

    }
}